package com.sxj.corejava.code08_常用类.String;

/**
 * @author 石小俊
 * @date 2024年04月13日 10:05
 */
public class Test13_性能比较 {

    public static void main(String[] args) {
        testString();
        testStringBuffer();
        testStringBuilder();
    }

    public static void testString() {
        long begin = System.currentTimeMillis();
        String s = "";
        for (int i = 0; i < 10000; i++) {
            s += "hello";
        }
        long end = System.currentTimeMillis();
        System.out.println("String操作了1W次耗费了" + (end - begin) + "毫秒");
    }

    public static void testStringBuffer() {
        long begin = System.currentTimeMillis();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < 1000000; i++) {
            buffer.append("hello");
        }
        long end = System.currentTimeMillis();
        System.out.println("StringBuffer操作了100W次耗费了" + (end - begin) + "毫秒");
    }

    public static void testStringBuilder() {
        long begin = System.currentTimeMillis();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < 1000000; i++) {
            builder.append("hello");
        }
        long end = System.currentTimeMillis();
        System.out.println("StringBuilder操作了100W次耗费了" + (end - begin) + "毫秒");
    }
}
